package com.idcheck.rechcustoms;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 149-201605 on 2016/8/1.
 */

/*
* 主界面显示 dialog的资源适配器
* */
public class CabDetailAdapter extends BaseAdapter {

    private List<CabDetailModel> mList;

    private Context mContext;

    private String keyword;

    public CabDetailAdapter(Context mContext, List<CabDetailModel> mList) {
        this.mList = mList;
        this.mContext = mContext;


    }



    @Override
    public int getCount() {
        int count = 0;
        if (null != mList && !mList.isEmpty()) {
            count = mList.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder showDialogHolder = null;
        if (null == convertView) {
            showDialogHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.cab_detail_item, null);
            showDialogHolder.name = (TextView) convertView.findViewById(R.id.title);
            showDialogHolder.title_layout = (RelativeLayout) convertView.findViewById(R.id.title_layout);
            convertView.setTag(showDialogHolder);
        } else {
            showDialogHolder = (ViewHolder) convertView.getTag();
        }

        final int index = position;
//
        if (null != mList.get(position).getGridnum()) {

            showDialogHolder.name.setText(mList.get(position).getGridnum());

            if (!mList.get(position).isChoose()) {
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_kongxian_cor));
            } else {
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_jinyong_cor));
            }


            /*if(null!=mList.get(position).getIsuse()&&0==Integer.valueOf(mList.get(position).getIsuse())){
                //使用
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_shiyong_cor));

            }else if(null!=mList.get(position).getIsuse()&&1==Integer.valueOf(mList.get(position).getIsuse())){
                //空闲
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_kongxian_cor));
            }else if(null!=mList.get(position).getIsuse()&&2==Integer.valueOf(mList.get(position).getIsuse())){
                //禁用
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_jinyong_cor));
            }else if(null!=mList.get(position).getIsuse()&&3==Integer.valueOf(mList.get(position).getIsuse())){
                //内部使用
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_neibu_cor));
            }else if(null!=mList.get(position).getIsuse()&&4==Integer.valueOf(mList.get(position).getIsuse())){
                //内部使用
                showDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_neibu_cor));
            }*/
        }

        final ViewHolder finalShowDialogHolder = showDialogHolder;
        showDialogHolder.title_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //finalShowDialogHolder.title_layout.setBackground(mContext.getResources().getDrawable(R.drawable.cab_jinyong_cor));
                mList.get(position).setChoose(true);
                for (int i = 0; i < mList.size(); i++) {
                    if (i!=position) {
                        mList.get(i).setChoose(false);
                    }
                }
                notifyDataSetChanged();
            }
        });


        return convertView;
    }


    class ViewHolder {
        TextView name;
        RelativeLayout title_layout;
    }
}


